package helloworld.example.insert;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import helloworld.example.customer.CustomerDetailObject;
import helloworld.example.customer.delegate.*;
import helloworld.example.*;
import helloworld.example.customer.*;

/**
 * insert a customer
 * @author tuyen-dm
 * @author tuyen-dm
 */
public class CustomerInsertAction extends Action{

	/**
	 * 
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception{
		ActionForward forward = null;
		CustomerForm insertform = (CustomerForm)form;
		if (insertform.getCancel().isSelected()) 
	     {
	          System.out.println("Cancel Operation Performed");
	          forward = mapping.findForward("mainpage");
	     }
		  
		if(insertform.getSave().isSelected()){
			CustomerDetailObject customer = new CustomerDetailObject();			
			
			Address address = insertform.getAddress();			
			customer.setAddress(address);
			customer.setEmailAddress(insertform.getEmailAddress());
			customer.setFirstName(insertform.getFirstName());
			customer.setLastName(insertform.getLastName());
			customer.setPreferredCarrier(insertform.getPreferredCarrier());
			customer.setReceiveEmail(insertform.getReceiveEmail());
			
			CustomerDelegate delegate = new CustomerDelegate();
			int res = delegate.createCustomer(customer);
			if(res == -1){
				insertform.setIsExisting(true);
			}				
			
			forward = mapping.findForward("insert-success");
		}
		return forward;		
	}
}
